package com.example.controller;


import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;

import static org.springframework.security.oauth2.client.web.reactive.function.client.ServletOAuth2AuthorizedClientExchangeFilterFunction.oauth2AuthorizedClient;

@RestController
public class TestClientController {
    private final WebClient defaultClientWebClient;

    public TestClientController(
            @Qualifier("default-client-web-client") WebClient defaultClientWebClient
    ) {
        this.defaultClientWebClient = defaultClientWebClient;
    }



    // http://localhost:9111/client/hello
    // 网关访问
    // http://localhost:8080/client/hello
    @GetMapping("hello")
//    public String who(@RegisteredOAuth2AuthorizedClient("password-client-authorization-code")
    public String who(@RegisteredOAuth2AuthorizedClient("password-client-credentials")
    OAuth2AuthorizedClient authorizedClient) {
        String messages = this.defaultClientWebClient
                .get()
//                .uri("http://localhost:9526/demo/hello")
                .uri("http://localhost:8001/sys/hello")
                .attributes(oauth2AuthorizedClient(authorizedClient))
                .retrieve()
                .bodyToMono(String.class)
                .block();
        return messages;
    }

}
